﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :D_T_MicroBlogging_Exp
//        description :微博数据访问类
//
//        created by JOE at  2010年12月31日 09:50 
//
//======================================================================
namespace huaguwang
{
      /// <summary>
      /// 微博数据访问类
      /// </summary>
      public sealed class D_T_MicroBlogging_Exp : D_T_MicroBlogging
      {
            public class MicroBolgExp : huaguwang.M_T_MicroBlogging
            {
                  /// <summary>
                  /// 会员姓名
                  /// </summary>
                  public string MemberName { get; set; }
            }

            /// <summary>
            /// 根据会员姓名模糊查询会员微博信息列表
            /// </summary>
            /// <param name="memberName">会员姓名</param>
            /// <param name="begin">查询起始数量</param>
            /// <param name="pageSize">分页大小</param>
            /// <param name="QueryCount">输出查询数量</param>
            /// <returns>符合条件查询列表</returns>
            public List<MicroBolgExp> GetConditionMicroBolgs(string memberName, int begin, int pageSize, out int queryCount)
            {
                  string strTable = "dbo.T_MicroBlogging as a inner join dbo.T_Member as b on a.MemberID = b.id";
                  string strWhere = "a.Flag = 1 and  b.Name like '%'+@MemberName+'%'  and b.Flag = 1";
                  string strQuerycountSQL = ExpandMSSQL.GetPageSQL(strTable, strWhere);
                  SqlParameter[] sqlParam = new SqlParameter[]
                  {
                        new SqlParameter("@MemberName",SqlDbType.NVarChar,50){ Value=memberName},
                  };
                  queryCount = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strQuerycountSQL, sqlParam).Exp_IntTryParse();

                  string strSQL = ExpandMSSQL.GetPageSQL(begin, pageSize, "a.*,b.Name as membername", strTable, "a.id", strWhere, "a.AddDate desc,b.Name");
                  var vResult = MSSQLHelper.SQLConType.C1.Exp_GetList<MicroBolgExp>(strSQL, row => new MicroBolgExp
                  {
                        id = row["id"].Exp_IntTryParse(),
                        AddDate = row["AddDate"].Exp_DateTimeTryParse(),
                        Deblocking = row["Deblocking"].Exp_BoolTryParse(),
                        Flag = true,
                        IP = row["IP"].Exp_Trim(),
                        TopIs = row["TopIs"].Exp_BoolTryParse(),
                        Topic = row["Topic"].Exp_Trim(),
                        RemSign = row["RemSign"].Exp_BoolTryParse(),
                        MemberName = row["membername"].Exp_Trim()
                  }, sqlParam);
                  return vResult;
            }

            /// <summary>
            /// 批量封帖
            /// </summary>
            /// <param name="lstMicroBlogID">微博列表ID</param>
            ///<param name="FrozenType">封帖,解帖,删除类型</param> 
            /// <returns>返回受影响的行数</returns>
            public int ChangeMicroBolgState(List<int> lstMicroBlogID, FrozenType type)
            {
                  var lstID = lstMicroBlogID.Exp_JoinList<int>(",", id => id.Exp_Trim());
                  int executeQuery = 0;
                  switch (type)
                  {
                        case FrozenType.删除:
                              executeQuery = base.UpdateFlag(lstMicroBlogID);
                              break;
                        case FrozenType.封帖:
                              executeQuery = this.BlockingBlog(lstID);
                              break;
                        case FrozenType.解帖:
                              executeQuery = this.DeblockingBlog(lstID);
                              break;
                        case FrozenType.推荐:
                              executeQuery = this.RemSignTrue(lstID);
                              break;
                        case FrozenType.置顶:
                              executeQuery = this.TopIsTrue(lstID);
                              break;
                        case FrozenType.不推荐:
                              executeQuery = this.RemSingFlase(lstID);
                              break;
                        case FrozenType.不置顶:
                              executeQuery = this.TopIsFalse(lstID);
                              break;
                  }
                  return executeQuery;
            }
            //封帖  设置为0
            private int BlockingBlog(string strID) 
            {
                  var strSql = new StringBuilder("update dbo.T_MicroBlogging set Deblocking = 0 where id in(" + strID.Exp_Trim() + ") and flag = 1");
                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString());
                  return rows;
            }
            //解帖  设置为1
            private int DeblockingBlog(string strID) 
            {
                  var strSql = new StringBuilder("update dbo.T_MicroBlogging set Deblocking = 1 where id in(" + strID.Exp_Trim() + ") and flag = 1");
                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString());
                  return rows;
            }

            //置顶
            private int TopIsTrue(string strID)
            {
                  var strSql = new StringBuilder("update dbo.T_MicroBlogging set TopIs = 1 where id in(" + strID.Exp_Trim() + ") and flag = 1");
                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString());
                  return rows;
            }

            //不置顶
            private int TopIsFalse(string strID) 
            {
                  var strSql = new StringBuilder("update dbo.T_MicroBlogging set TopIs = 0 where id in(" + strID.Exp_Trim() + ") and flag = 1");
                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString());
                  return rows;
            }

            //推荐
            private int RemSignTrue(string strID) 
            {
                  var strSql = new StringBuilder("update dbo.T_MicroBlogging set RemSign = 1 where id in(" + strID.Exp_Trim() + ") and flag = 1");
                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString());
                  return rows;
            }

            //不推荐
            private int RemSingFlase(string strID) 
            {
                  var strSql = new StringBuilder("update dbo.T_MicroBlogging set RemSign = 0 where id in(" + strID.Exp_Trim() + ") and flag = 1");
                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString());
                  return rows;
            }

            /// <summary>
            /// 得到一个对象实体
            /// </summary>
            public new  MicroBolgExp GetModel(int id)
            {

                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("select  top 1 a.* ,b.Name  from T_MicroBlogging as a inner join T_Member as b on a.MemberID = b.id ");
                  strSql.Append(" where a.id=@id");
                  SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
                  };
                  parameters[0].Value = id;

                  MicroBolgExp model = new MicroBolgExp();
                  DataSet ds = MSSQLHelper.SQLConType.C1.Exp_Query(strSql.ToString(), parameters);
                  if (ds.Tables[0].Rows.Count > 0)
                  {
                        if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                        {
                              model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                        }
                        if (ds.Tables[0].Rows[0]["MemberID"].ToString() != "")
                        {
                              model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                        }
                        model.Topic = ds.Tables[0].Rows[0]["Topic"].ToString();
                        if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                        {
                              model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                        }
                        model.IP = ds.Tables[0].Rows[0]["IP"].ToString();
                        if (ds.Tables[0].Rows[0]["TopIs"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["TopIs"].ToString() == "1") || (ds.Tables[0].Rows[0]["TopIs"].ToString().ToLower() == "true"))
                              {
                                    model.TopIs = true;
                              }
                              else
                              {
                                    model.TopIs = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["RemSign"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["RemSign"].ToString() == "1") || (ds.Tables[0].Rows[0]["RemSign"].ToString().ToLower() == "true"))
                              {
                                    model.RemSign = true;
                              }
                              else
                              {
                                    model.RemSign = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["Deblocking"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["Deblocking"].ToString() == "1") || (ds.Tables[0].Rows[0]["Deblocking"].ToString().ToLower() == "true"))
                              {
                                    model.Deblocking = true;
                              }
                              else
                              {
                                    model.Deblocking = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["Flag"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["Flag"].ToString() == "1") || (ds.Tables[0].Rows[0]["Flag"].ToString().ToLower() == "true"))
                              {
                                    model.Flag = true;
                              }
                              else
                              {
                                    model.Flag = false;
                              }
                        }
                        model.MemberName = ds.Tables[0].Rows[0]["Name"].ToString();
                        return model;
                  }
                  else
                  {
                        return null;
                  }
            }

            public enum FrozenType
            {
                  封帖,
                  解帖,
                  删除,
                  推荐,
                  置顶,
                  不推荐,
                  不置顶
            }
      }
}
